"""
状态模式: 以水的三中状态为例子
    Water --- State(SolidState LiquidState GaseousState)
author: Mr_52Hz
date: 2020-03-09
"""
from abc import ABCMeta, abstractmethod


class State(metaclass=ABCMeta):

    def __init__(self, state='Solid'):
        self.state = state

    @abstractmethod
    def feature(self):
        pass


class SolidState(State):

    def __init__(self):
        super().__init__('solid')

    def feature(self):
        print('我是固体')


class LiquidState(State):

    def __init__(self):
        super(LiquidState, self).__init__('Liquid')

    def feature(self):
        print('我是液体')


class GaseousState(State):

    def __init__(self):
        super().__init__('Gaseous')

    def feature(self):
        print('我是气体')


class Water(object):

    def __init__(self, state=LiquidState()):
        if isinstance(state, State):
            self.__state = state
        else:
            raise ValueError('参数state: %s错误' % state)
        self.__temperature = 17

    @property
    def temperature(self):
        return '%s' % self.__temperature + '℃'

    @temperature.setter
    def temperature(self, temp):
        if isinstance(temp, str) and temp.isalnum():
            temp = int(temp)
        elif isinstance(temp, (int, float)):
            temp = temp
        else:
            raise ValueError('设置temperature: %s错误' % temp)
        self.__temperature = temp
        self.changeState()

    @property
    def state(self):
        self.__state.feature()
        return None

    def changeState(self, ):
        if self.__temperature < 0:
            self.__state = SolidState()
        elif self.__temperature > 100:
            self.__state = GaseousState()
        else:
            self.__state = LiquidState()


if __name__ == '__main__':
    water = Water()
    water.state
    water.temperature = -1
    water.state
    water.temperature = 50
    water.state
    water.temperature = 101
    water.state



